import PreviewAPI from '@/api/preview'

let requestList = {}
let time_count = 1000
let isFlag = false
let responseList = {}
export default (params, callback) => {
  if (responseList[params]) {
    callback(responseList[params])
    return
  }
  if (requestList[params]) {
    requestList[params].push(callback)
  } else {
    requestList[params] = [callback]
  }
  if (isFlag) return
  isFlag = true
  setTimeout(() => {
    debugger
    Promise.all(
      Object.keys(requestList).map(async item => {
        return {
          key: item,
          data: await PreviewAPI.getDictionary(item, 'category')
        }
      })
    )
      .then(result => {
        isFlag = false
        result.forEach(item => {
          requestList[item.key].forEach(fn => {
            fn(item.data.data.msg)
          })
          responseList[item.key] = item.data.data.msg
        })
        requestList = {}
      })
      .catch(() => {
        isFlag = false
        requestList = {}
      })
  }, time_count)
}
